from typing import List

from rest_framework import serializers
from ninja import ModelSchema

from models.models import Goods, GoodPicture, GoodSpecification, GoodType
from water_applets import settings


class GoodSpecificationSerializer(serializers.ModelSerializer):
    """
    商品规格
    """

    class Meta:
        model = GoodSpecification
        fields = "__all__"


class GoodSimpleSerializer(serializers.ModelSerializer):
    """
    商品
    """
    picture = serializers.SerializerMethodField()
    specs = serializers.SerializerMethodField()
    type = serializers.SerializerMethodField()

    def get_picture(self, obj):
        """
        获取商品图片
        """
        if not GoodPicture.objects.filter(good=obj).exists():
            return ""
        header = "http://"
        if settings.WEB_SERVER_IS_HTTPS:
            header = "https://"
        return f'{header}{settings.WEB_SERVER_IPV4}:{settings.WEB_SERVER_PORT}/{GoodPicture.objects.filter(good=obj).values_list("picture", flat=True)[0]}'

    def get_specs(self, obj):
        """
        获取商品规格
        """
        if not GoodSpecification.objects.filter(good__id=obj.id).exists():
            return 0
        return GoodSpecification.objects.filter(good__id=obj.id)[0].price

    def get_type(self, obj):
        """
        获取商品类型
        """
        return GoodTypeSerializer(obj).data

    class Meta:
        model = Goods
        fields = ["id", "name", "picture", "specs", "buyers_number", "type"]


class GoodSerializer(serializers.ModelSerializer):
    """
    商品
    """
    picture = serializers.SerializerMethodField()
    specs = serializers.SerializerMethodField()
    type = serializers.SerializerMethodField()

    def get_picture(self, obj):
        """
        获取商品图片
        """
        header = "http://"
        if settings.WEB_SERVER_IS_HTTPS:
            header = "https://"
        return [f'{header}{settings.WEB_SERVER_IPV4}:{settings.WEB_SERVER_PORT}/{item}' for item in list(GoodPicture.objects.filter(good=obj).values_list("picture", flat=True))]

    def get_specs(self, obj):
        """
        获取商品规格
        """
        return GoodSpecificationSerializer(GoodSpecification.objects.filter(good__id=obj.id), many=True).data

    def get_type(self, obj):
        """
        获取商品类型
        """
        return GoodTypeSerializer(obj).data

    class Meta:
        model = Goods
        fields = "__all__"


class GoodSpecificationModelSchema(ModelSchema):
    class Config:
        model = GoodSpecification
        model_fields = "__all__"


class GoodsModelSchema(ModelSchema):
    """
    商品
    """

    class Config:
        model = Goods
        model_fields = "__all__"


class GoodsResponseSchema(ModelSchema):
    """
    商品
    """
    # 需要指定外键的related_name=specs， 下面的模型字段才能被序列化
    # good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name=_("商品"), related_name="specs")
    specs: List[GoodSpecificationModelSchema] = []

    class Config:
        model = Goods
        model_fields = "__all__"


class GoodTypeSerializer(serializers.ModelSerializer):
    """
    商品类型
    """

    class Meta:
        model = GoodType
        fields = "__all__"
